package delta;

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

import org.junit.Before;
import org.junit.Test;

public class InputStreamComparator_utest {
	Charset utf8_;
	InputStreamComparator comparator_;

	@Before
	public void setUp() {
		utf8_ = Charset.forName("UTF-8");
		comparator_ = new InputStreamComparator();
	}

	@Test
	public void testEmpty() throws IOException {
		compare("", "");
	}

	@Test
	public void testIdentical() throws IOException {
		compare("abc", "abc");
	}

	@Test
	public void testDifferentContent() throws IOException {
		compare("abc", "abd");
	}

	@Test
	public void testDifferentSize() throws IOException {
		compare("abc", "abcd");
	}

	void compare(String a, String b) throws IOException {
		assertEquals(a + " ? " + b, Integer.signum(a.compareTo(b)), Integer.signum(comparator_.compare(s(a), s(b))));
		assertEquals(b + " ? " + a, Integer.signum(b.compareTo(a)), Integer.signum(comparator_.compare(s(b), s(a))));
	}

	InputStream s(String str) {
		return new ByteArrayInputStream(str.getBytes(utf8_));
	}
}
